package org.itboys.core.service.mongo.web;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.entity.mongo.SundayCommonMessage;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/8
 * Description: 常用问题服务类
 */

@Service
public class SundayCommonMessageService extends BaseMongoService<SundayCommonMessage> {
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<SundayCommonMessage> getEntityClass() {
        return SundayCommonMessage.class;
    }

    /**
     * 分页查询常用问题
     * 支持模糊查询字段:
     * 问题关键字; 内容关键字;
     *
     * @param request
     * @return
     */
    public PageResult<SundayCommonMessage> select(HttpServletRequest request) {
        //模糊查询条件: 问题标题. 问题内容
        Map<String, String> containsparam = ParamUtil.packageMongoVagueParam(
                request,
                "name",
                "text");
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        //添加查询条件, 后续加入创建人, 创建时间, 修改时间顺序
        param.put("isDeleted", 0);

        PageResult<SundayCommonMessage> result = super.containpageQuery(request, containsparam, param);
        return result;
    }

    /**
     * 查找单个常用问答
     *
     * @param id
     * @return
     */
    public SundayCommonMessage findOne(Long id) {
        SundayCommonMessage question = null;

        if (LongUtil.isNotZreo(id)) {
            question = super.getById(id);
        } else {
            question = new SundayCommonMessage();
        }
        return question;
    }

    /**
     * 保存常见问答
     *
     * @param question
     */
    public void save(SundayCommonMessage question) {
        if (LongUtil.isZreo(question.getId())) {
            super.save(question);
        } else {
            super.updateExceptEmpty(question.getId(), question);
        }
    }

    /**
     * 设置是否已经通过该常见问答
     *
     * @param id
     * @param status
     */
    public void setStatus(Long id, Integer status) {
        super.update(id, "status", status);
    }

}
